//
//  WPTextView.m
//  Weibo
//
//  Created by wpapple on 16/6/3.
//  Copyright © 2016年 wpapple. All rights reserved.
//

#import "WPTextView.h"

@implementation WPTextView

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self setUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
    }
    return self;
}

-(void)setUI
{
    UILabel *placeHolderLabel = [[UILabel alloc]init];
    
    self.placeHolderLabel = placeHolderLabel;
    
    [self addSubview:placeHolderLabel];
    
    placeHolderLabel.text = @"写点什么吧";
    
    [placeHolderLabel setTextColor:[UIColor lightGrayColor]];
    
    self.placeHolderLabel.numberOfLines = 0;
                             
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeText:) name:UITextViewTextDidChangeNotification object:nil];
    
    self.alwaysBounceVertical = YES;
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

-(void)changeText:(NSNotification *)notification
{
    if (self.text.length > 0) {
        self.placeHolderLabel.text = @"";
    }else
    {
        self.placeHolderLabel.text = @"写点什么";
    }
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    self.placeHolderLabel.x = 2;
    self.placeHolderLabel.y = 5;
    
    [self.placeHolderLabel sizeToFit];
}

-(void)setFont:(UIFont *)font
{
    [super setFont:font];
    
    [self.placeHolderLabel setFont:font];
}

-(void)setPlaceHolder:(NSString *)placeHolder
{
    self.placeHolderLabel.text = placeHolder;
    
}

@end
